layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
}).use(['index', 'table', 'laytpl', 'laydate'], function(){
    var $ = layui.$,
        table = layui.table,
        laydate = layui.laydate;

    laydate.render({
        elem: '#time', //指定元素
        type: 'datetime',
        range: true
    });

    table.render({
        id: 'data-table',
        elem: '#data-table',
        url: 'operate/im/view',
        where: getData(),
        toolbar: '#data-table-toolbar',
        title: '聊天记录列表',
        height: '900',
        page: true,
        cols: [[
            {field: 'id', title: 'ID', width: 100, align: 'center'},
            {field: 'fromUserId', title: '用户id', width: 160, align: 'center', style: 'color: blue', templet: function (d){
                    var fromUserId = d.fromUserId;
                    if(searchData.fromUserId != null && searchData.fromUserId != ''){
                        fromUserId = searchData.fromUserId;
                    }
                    return fromUserId;
                }
            },
            {field: 'msg', title: '消息内容', align: 'center', templet:
                function (d) {
                    var str = d.msg;
                    if(d.type == 'PICTURE'){
                        str = '' + ossDomain + '<img src="/" style="width: 100%;">' + d.msg + '';
                    }else if(d.type == 'CUSTOM'){
                        if(d.attach.type == 743){
                            str = '' + ossDomain + '<img src="/" style="width: 100%;">' + d.msg + '';
                        }else{
                            str = '自定义表情，无法显示';
                        }
                    }else if(d.type == 'AUDIO') {
                        str = '<audio controls src="' + d.msg + '"/>';
                    }else if(d.type == 'TEXT') {
                        if(d.keywords !=''){
                            var keywordArr = d.keywords.split(",");

                            $.each(keywordArr, function (index, ele){
                                str = str.replace(ele, '<span style="color: red">' +ele + '</span>');
                            });
                        }
                        str = '<span style="font-family: auto">' + str + '</span>';
                    }
                    if(d.fromUserId == searchData.fromUserId){
                        str = '<div style="text-align: left">' + str + '</div>';
                    }else{
                        str = '<div style="text-align: right">' + str + '</div>';
                    }
                    return str;
                }
            },
            {field: 'toUserId', title: '用户id', align: 'center', width: 160, style: 'color: red', templet: function (d){
                    var toUserId = d.toUserId;
                    if(searchData.toUserId != null && searchData.toUserId != ''){
                        toUserId = searchData.toUserId;
                    }
                    return toUserId;
                }
            },
            {field: 'createTime', title: '发送时间', width: 160, align: 'center'},
            {field: 'op', title: '操作', width: 160, align: 'center', templet: '#data-table-bar'}
        ]],
        parseData: function(result){
            return {
                "code": result.code, //解析接口状态
                "msg": result.msg, //解析提示文本
                "count": result.data.total, //解析数据长度
                "data": result.data.datas //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(data-table)', function(obj){
        switch(obj.event){
            case 'search':
                var data = getData();
                table.reload('data-table', {
                    url: 'operate/im/view',
                    where: data,
                    page: {
                        curr: 1
                    }
                });
                reloadData(data);
                break;
        };
    });

    //监听行工具事件
    table.on('tool(data-table)', function(obj){
        var data = obj.data;
        if(obj.event === 'updateStatus'){
            layer.confirm('确定处理该消息', {offset: '30%'}, function(index){
                var ids = [];
                ids.push(data.id);
                // 加载层
                var load = layerLoad();
                post('operate/im/updateStatus', {'ids': ids}, false, function (data) {
                    table.reload('data-table');
                    layer.msg("处理成功");
                    layer.close(index);
                    layer.close(load);
                });
            });
        }
    });

    /**
     * 获取查询数据
     */
    var searchData;
    function getData() {
        var fromUserId;
        if($('#fromUserId').length == 0){
            fromUserId = $($('#data-table-toolbar ').html()).find('#fromUserId').val();
        }else{
            fromUserId = $('#fromUserId').val();
        }
        var toUserId;
        if($('#toUserId').length == 0){
            toUserId = $($('#data-table-toolbar ').html()).find('#toUserId').val();
        }else{
            toUserId = $('#toUserId').val();
        }
        var time = $('#time').val();

        searchData = {'fromUserId': fromUserId, 'toUserId': toUserId, 'time': time};
        return searchData;
    }

    /**
     * 重新渲染数据
     * @param data
     */
    function reloadData(data) {
        $('#fromUserId').val(data.fromUserId);
        $('#toUserId').val(data.toUserId);
        $('#time').val((data.time));
        laydate.render({
            elem: '#time', //指定元素
            type: 'datetime',
            range: true
        });
    }
});